﻿
using SIG.Basic.Extensions;
using SIG.Infrastructure.Cache;
using SIG.Infrastructure.Configs;
using SIG.Infrastructure.Logging;
using SIG.Model.DataAcquisition;
using SIG.Model.Emails;
using SIG.Repository;
using SIG.Resources.Admin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace SIG.Service
{
    public class FilterTemplateService : EntityService<FilterTemplate>, IFilterTemplateService
    {

        private readonly IFilterTemplateRepository _articleRepository;
        private readonly ILoggingService _loggingService;
        private readonly ICacheService _cacheService;

        public FilterTemplateService(IFilterTemplateRepository articleRepository, ILoggingService loggingService, ICacheService cacheService)
            : base(articleRepository, cacheService, loggingService)
        {

            _articleRepository = articleRepository;
            _loggingService = loggingService;
            _cacheService = cacheService;
        }



        public List<FilterTemplate> GetPagedElements(int pageIndex, int pageSize, string keyword, out int totalCount)
        {

            //get list count
            var keyCount = $"{EntityNames.FilterTemplate}_ListCount_{keyword}";

            var totalIQuery = _articleRepository.GetAllElements();
            if (!string.IsNullOrEmpty(keyword))
                totalIQuery = totalIQuery.Where(g => g.Name.Contains(keyword));
         

            if (SettingsManager.Store.EnableCaching)
            {
                if (_cacheService.IsSet(keyCount))
                {
                    totalCount = (int)_cacheService.Get(keyCount);
                }
                else
                {
                    totalCount = totalIQuery.Count();
                    _cacheService.Set(keyCount, totalCount, SettingsManager.Store.CacheDuration);
                }
            }
            else
            {
                totalCount = totalIQuery.Count();
            }


            //get list
            var key = $"{EntityNames.FilterTemplate}_List_{pageIndex}_{pageSize}_{keyword}";

            List<FilterTemplate> articles;
            Expression<Func<FilterTemplate, bool>> filter = g => true;
            Expression<Func<FilterTemplate, bool>> filterByKeyword = g => g.Title.Contains(keyword);
     

            if (!string.IsNullOrEmpty(keyword))
                filter = filter.AndAlso(filterByKeyword);
       


            if (SettingsManager.Store.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    articles = (List<FilterTemplate>)_cacheService.Get(key);
                }
                else
                {
                    articles = _articleRepository.GetPagedElements(pageIndex, pageSize, (c => c.CreatedDate), filter, false).ToList();
                    _cacheService.Set(key, articles, SettingsManager.Store.CacheDuration);
                }
            }
            else
            {
                articles = _articleRepository.GetPagedElements(pageIndex, pageSize, (c => c.CreatedDate), filter, false).ToList();
            }

            return articles;

        }

       

       
    }
}
